#include "MainForm.h"


using namespace Osp::Base;
using namespace Osp::Ui;
using namespace Osp::Ui::Controls;
using namespace Osp::Xml;
using namespace Osp::Base::Utility;

MainForm::MainForm(void)
{
}

MainForm::~MainForm(void)
{
}

bool
MainForm::Initialize()
{
	// Construct an XML form
	Construct(L"IDF_MAINFORM");

	return true;
}

result
MainForm::OnInitializing(void)
{
	result r = E_SUCCESS;

	// Get a button via resource ID
	__pButtonGo = static_cast<Button *>(GetControl(L"IDC_BUTTON_OK"));
	if (__pButtonGo != null)
	{
		__pButtonGo->SetActionId(ID_BUTTON_GO);
		__pButtonGo->AddActionEventListener(*this);
	}

	__pMyList = static_cast<List *>(GetControl(L"MYLIST"));

	return r;
}

result
MainForm::OnTerminating(void)
{
	result r = E_SUCCESS;
	delete doc;
	return r;
}

void MainForm::ParseAndWrite()
{
		// a dokumentum beolvasasa
		doc = xmlReadFile("/Home/test.xml", NULL, 0);
		if(doc==NULL)
		{
			AppLog("xml dokumentum betoltesi para");
		}

		/* kontextus felepitese */
		xpathContext = xmlXPathNewContext(doc);
		if(xpathContext == NULL) {
			AppLog("context para");
			xmlFreeDoc(doc);
			//return(E_IO);
		}

		/* xpath kifejezes kiertekelese */
		xpathObjKeszulek = xmlXPathEvalExpression((xmlChar*)"//nev", xpathContext);
		xpathObjAr = xmlXPathEvalExpression((xmlChar*)"//ar", xpathContext);
		if(xpathObjKeszulek == NULL || xpathObjAr == NULL) {
			AppLog("xpath kiertekelesi para");
			xmlXPathFreeContext(xpathContext);
			xmlFreeDoc(doc);
		}

		/*eredmenyhalmaz bejarasa*/
		int size;
		int i;
		String keszulek;
		String ar;
		size = (xpathObjKeszulek->nodesetval) ? xpathObjKeszulek->nodesetval->nodeNr : 0;
		AppLog("%d",size);
		for(i = 0; i <size; i++) {
			StringUtil::Utf8ToString((char*)xpathObjKeszulek->nodesetval->nodeTab[i]->children->content, keszulek);
			StringUtil::Utf8ToString((char*)xpathObjAr->nodesetval->nodeTab[i]->children->content, ar);
			__pMyList->AddItem(&keszulek,&ar,NULL,NULL,i);
		}

		this->RequestRedraw(true);

		xmlFreeDoc(doc);
		xmlCleanupParser();
}

void
MainForm::OnActionPerformed(const Osp::Ui::Control& source, int actionId)
{
	switch(actionId)
	{
	case ID_BUTTON_GO:
		{
			ParseAndWrite();
		}
		break;
	default:
		break;
	}
}
